package ru.compscicenter.uno.wizard;

import ru.compscicenter.uno.GameSettings;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * User: SLeeP
 * Date: 03.03.12
 * Time: 4:34
 */
public class Panel1Descriptor extends WizardPanelDescriptor implements DocumentListener, ActionListener {
    public static final String IDENTIFIER = "INTRODUCTION_PANEL";
    
    private static Panel1 panel1;
    private GameSettings gameSettings;

    public Panel1Descriptor(GameSettings gameSettings) {
        panel1 = new Panel1(gameSettings);
        panel1.addNameListener(this);
        panel1.addRadioButtonChangeListener(this);
        setPanelDescriptorIdentifier(IDENTIFIER);
        setPanelComponent(panel1);
        this.gameSettings = gameSettings;
    }

    public Object getNextPanelDescriptor() {
        checkName();
        if (panel1.getMode().equals(Panel1.IS_SERVER)) {
            return Panel2ServerRulesDescriptor.IDENTIFIER;
        } else {
            return Panel2ClientDescriptor.IDENTIFIER;
        }
    }

    public Object getBackPanelDescriptor() {
        return null;
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        checkName();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        checkName();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        checkName();
    }

    private void checkName() {
        gameSettings.name = Panel1.getPlayerName();
        boolean isOk = true;

        if (!gameSettings.name.matches("[A-Za-z0-9]*")) {
            isOk = false;
        }

        if (gameSettings.name.isEmpty() || !isOk) {
            getWizard().setNextFinishButtonEnabled(false);
        } else {
            getWizard().setNextFinishButtonEnabled(true);
        }
//        Main_wizard.clientController.getPlayer(Main_wizard.clientController.getCurrentPlayerId()).setName(name);
//        Main_wizard.serverController.getPlayer(Main_wizard.serverController.getCurrentPlayerId()).setName(name);
    }

    public void actionPerformed(ActionEvent e) {
        if (panel1.getRadioButtonSelected().equals(Panel1.IS_SERVER)) {
            gameSettings.mode = Panel1.IS_SERVER;
        } else {
            gameSettings.mode = Panel1.IS_CLIENT;
        }
    }
}
